﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Upeu.Venta.Data.Entities;

namespace Upeu.Venta.Data.Business
{
    public class ClienteBusiness
    {
        //CRUD + Logic business or constrains
        public static IList<Cliente> GetListByFilter(string filter)
        {
            //throw new NotImplementedException();
            using (UpeuVentaDbContext ctx = new UpeuVentaDbContext()) // conexion a la db
            {
                return ctx.Clientes.Where(t => t.Nombre.Contains(filter)).ToList(); //Clientes tiene CRUD
            }
        }
        public static Cliente Create(Cliente cliente)
        {
            using (UpeuVentaDbContext ctx = new UpeuVentaDbContext()) 
            {
                Cliente clienteDB= ctx.Clientes.Add(cliente);
                ctx.SaveChanges();
                return clienteDB;
            }
        }
        public static Cliente Update(Cliente cliente)
        {
            using (UpeuVentaDbContext ctx = new UpeuVentaDbContext())
            {
                ctx.Entry(cliente).State = EntityState.Modified;
                ctx.SaveChanges();
                return cliente;
            }
        }
        public static Cliente GetById(Guid id)
        {
            using (UpeuVentaDbContext ctx = new UpeuVentaDbContext())
            {
                Cliente c = ctx.Clientes.Find(id);
                if (c == null)
                {
                    throw new Exception("Cliente no found in data base.");
                }
                return c;
            }
        }
        public static void Delete(Guid id)
        {
            using (UpeuVentaDbContext ctx = new UpeuVentaDbContext())
            {
                Cliente c = ctx.Clientes.Find(id);
                if (c == null)
                {
                    throw new Exception("Cliente no found in data base.");
                }
                ctx.Clientes.Remove(c);
                ctx.SaveChanges();
            }
        }
    }
}
